﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using YYTea.MOdel.Model;
using YYTea.ORM;
using YYTea.Services.IServices;
using YYTea.Services.VModel;

namespace YYTea.Services.Services
{
    public class ProductServices : IProduct
    {
        public readonly YYTeaContext _context;
        private readonly ConcurrentDictionary<Guid, Product> _Product = new ConcurrentDictionary<Guid, Product>();
        public ProductServices(YYTeaContext context)
        {
            _context = context;
        }
        public async Task<Product> AddProduct(ProductVModel productVModel)
        {
            Product product = new Product();
            if (product == null)
            {
                return null;
            }
            //if(product.ProductID == productVModel.ProductID)
            //{
            //    return 
            //}
            //productVModel.ProductID = Guid.NewGuid();
            //var pro = await _context.Products.ToListAsync(); 
            product.ProductID = Guid.NewGuid();
            //product.NumberSe += i;
            product.Name = productVModel.Name;
            product.StorkNum = productVModel.Num;
            product.Price = productVModel.Price;
            product.Img = productVModel.Img;
            product.Detail = productVModel.Detail;
            await _context.Products.AddAsync(product);
            await _context.SaveChangesAsync();
            return product;
        }

        public async Task<Product> DeleteProduct(Guid ID)
        {
            //throw new NotImplementedException();
            Product product = await _context.Products.SingleOrDefaultAsync(s => s.ProductID == ID);
            if (product == null)
            {
                return null;
            }
            _context.Products.Remove(product);
            await _context.SaveChangesAsync();
            return product;

        }

        public async Task<List<Product>> GetAllProduct()
        {
            //throw new NotImplementedException();
            var all = await _context.Products.ToListAsync();
            return all;
        }

        public async Task<Product> GetProductByID(Guid ID)
        {
            if (ID == null)
            {
                return null;
            }
            //throw new NotImplementedException();
            var pro = await _context.Products.FirstOrDefaultAsync(p => p.ProductID == ID);
            return pro;
        }

        public async Task<List<Product>> GetProductByName(string Name)
        {
            if (Name == null)
            {
                return null;
            }
            var pro = await _context.Products.Where(s => s.Name.Contains(Name)).ToListAsync();
            return pro;
        }
        public async Task<Product> UpdateProduct(ProductVModel productVModel, Guid id)
        {
            Product pro = new Product();
            //var getId = await _context.Products.SingleOrDefaultAsync(s => s.ProductID == id);
            //if (getId == null)
            //{
            //    return null;
            //}
            if (productVModel == null)
            {
                return null;
            }
            pro.ProductID = id;
            pro.Name = productVModel.Name;
            pro.Detail = productVModel.Detail;
            pro.StorkNum = productVModel.Num;
            pro.Price = productVModel.Price;
            if (productVModel.Img != "")
            {
                pro.Img = productVModel.Img;
            }
            pro.Img = pro.Img;
            //_context.Products.Update(pro);
            //await _context.SaveChangesAsync();
            //return pro;

            //return null;
            var pp = _Product[pro.ProductID] = pro;
            _context.Products.Update(pp);
            await _context.SaveChangesAsync();
            return pro;
        }
    }
}
